//
//  RWGCDTimer.m
//  RWTool
//
//  Created by Ray on 2022/1/13.
//

#import "RWGCDTimer.h"

@interface RWGCDTimer() {
    dispatch_source_t _timer;
    BOOL _isFire;
}

@end

@implementation RWGCDTimer

/// 默认主线程创建
- (instancetype)initWithTimeInterval:(NSTimeInterval)interval
                              repeat:(BOOL)repeat
                               block:(dispatch_block_t)block {
    return [self initWithTimeInterval:interval repeat:repeat queue:dispatch_get_main_queue() block:block];
}


- (instancetype)initWithTimeInterval:(NSTimeInterval)interval
                              repeat:(BOOL)repeat
                               queue:(dispatch_queue_t)queue
                               block:(dispatch_block_t)block {
    
    // 1. 确定传进来的队列是必须存在的
    NSAssert(queue != NULL, @"queue can't be NULL while create TGCDTimer");
    
    if (self = [super init]) {
        // 2. 创建定时器
        _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
        // 3. 设置定时器的时间
        dispatch_source_set_timer(_timer, dispatch_time(DISPATCH_TIME_NOW, 0), interval * NSEC_PER_SEC, 0);
       
        // 4. 监听定时器的回调
        dispatch_source_set_event_handler(_timer, ^{
            // 将定时器的回调，传给外界
            if (block) {
                block();
            }
            // 如果不重复，执行一次之后就销毁定时器
            if (!repeat) {
                self->_isFire = NO;
                dispatch_source_cancel(self->_timer);
            }
        });
        
        // 5. 在给定的interval之后启动定时器
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(interval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            dispatch_resume(self->_timer);
            self->_isFire = YES;
//        });
    }
    
    return self;
}

+ (instancetype)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
                                        repeat:(BOOL)repeat
                                         queue:(dispatch_queue_t)queue
                                         block:(dispatch_block_t)block {
    return [[[self class] alloc] initWithTimeInterval:interval
                                               repeat:repeat
                                                queue:queue
                                                block:block];
}

/// 默认主线程创建
+ (instancetype)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
                                        repeat:(BOOL)repeat
                                         block:(dispatch_block_t)block {
    
    return [self scheduledTimerWithTimeInterval:interval
                                         repeat:repeat
                                          queue:dispatch_get_main_queue()
                                          block:block];
}


- (void)setTimeInterval:(NSTimeInterval)interval {
    
    
}

- (void)stop {
    if (_isFire) {
        _isFire = NO;
        dispatch_suspend(_timer);
    }
}

- (void)restart {
    if (!_isFire) {
        _isFire = YES;
        dispatch_resume(_timer);
    }
}

- (void)invalidate {
    _isFire = NO;
    dispatch_source_cancel(_timer);
}

- (void)dealloc {
    _isFire = NO;
    dispatch_source_cancel(_timer);
}

@end
